import Api, { HttpResponse } from '../../http/Api';
import { tokenInterceptor } from '../Interceptors';

export type PageCondition = {
	pageNum: number;
	pageSize: number;
	noPage: true;
};

export type OrderCondition = {
	columns: string[];
	mode: boolean;
};

export type RangeCondition<T> = {
	column: string;
	begin: T;
	end: T;
};

export type CategoryCondition<T = string | number> = {
	column: string;
	collection: T[];
};

export type TimeRangeCondition = RangeCondition<Date>;
export type NumberRangeCondition = RangeCondition<number>;

export type ColumnCondition = {
	timeConditions: TimeRangeCondition[];
	numberConditions: NumberRangeCondition[];
	categoryConditions: CategoryCondition[];
};

export interface QueryCondition<T> {
	query: T;
	pageCondition?: PageCondition;
	orderConditions?: OrderCondition[];
	columnCondition?: ColumnCondition;
}

export type QueryAllCondition = Omit<
	QueryCondition<unknown>,
	'query' | 'columnCondition'
>;

export interface HttpBodyEntity<T> {
	code: number;
	msg: string;
	data: T;
}

export type HttpServiceResponse<T> = HttpResponse<HttpBodyEntity<T>>;

export interface PageInfo<T> {
	records: T[];
	current: number;
	size: number;
	total: number;
	pages: number;
	searchCount: boolean;
}

export interface Model {
	id: string;
	createTime: string;
	updateTime: string;
	createUser: string;
	updateUser: string;
}

export interface TimeModel {
	id: string;
	createTime: Date;
	updateTime: Date;
	createUser: string;
	updateUser: string;
}

export abstract class Service extends Api {
	public constructor() {
		super();
		this.create();
	}

	protected abstract create(): Service;

	protected initTokenInstance = (key: string): Api => {
		return this.initSingle(
			key,
			{
				baseURL: `/api/${key}`,
			},
			tokenInterceptor
		) as Api;
	};
}
